﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WinFormTool
{
    /// <summary>
    /// 处理数据
    /// </summary>
    class ToolProcessingData
    {
        /// <summary>
        /// 处理int类型数据，错误是为0
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static int ParseInt(object obj)
        {
            int reInt = 0;
            if (obj != null)
                int.TryParse(obj.ToString(), out reInt);
            return reInt;
        }
        /// <summary>
        /// 处理DateTime类型数据，错误返回当前日期
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static DateTime ParseDateTime(object obj)
        {
            DateTime dateTime = DateTime.Now;
            if (obj!=null)
            {
                DateTime.TryParse(obj.ToString(), out dateTime);
            }
            return dateTime;
        }
        /// <summary>
        /// 处理String类型数据，错误返回string.Empty
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string ParseString(object obj)
        {
            string strString = string.Empty;
            if (obj != null)
            {
                strString = obj.ToString();
            }
            return strString;
        }
        public static int ParseComboxData(System.Windows.Forms.ComboBox cbx,string txt)
        {
            int reInt = 0;
            if (cbx.Items.Count>0&&(!string.IsNullOrEmpty(txt)))
            {
                for (int i = 0; i < cbx.Items.Count; i++)
                {
                    if (cbx.Items[i].ToString()==txt)
                    {
                        reInt = i;
                        break;
                    }
                }
            }
            return reInt;
        }
    }
}
